package com.ymatou.iapi.dglogin.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.captcha.parameter.GetValidateCodeBean;
import com.ymatou.iapi.captcha.service.GetValidateCodeCall;
import com.ymatou.iapi.dglogin.parameter.AppSourceEnum;
import com.ymatou.iapi.dglogin.parameter.MobileloginBean;
import com.ymatou.iapi.dglogin.service.MobileloginCall;
import com.ymatou.iapi.dguser.parameter.CreateUserBackBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.ValidateCodeServiceV2;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.utils.TestDataManager;
/**
*商家手机号登录
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Mobilelogin{
	private static MobileloginBean mobileloginBean;
	private static MobileloginCall mobileloginCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家手机号登录");
	}

	@Before
	public void caseUp() {
		mobileloginBean=new MobileloginBean();
		mobileloginCall=new MobileloginCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(mobileloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Mobilelogin_001() {
		Logger.start(true,"商家手机号登录成功");
		try {
//			confirmValidateCodeAndKeepNormalBean.setValidateCode(getValidateCodeCall.getValidateCode());
//			confirmValidateCodeAndKeepNormalBean.setValidateType(getvalidatecodeBean.getValidateType());
			
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setUserType(1);
			Integer user = DGUserCallService.UserCreateCall(u);
			DGUserCallService.ReceiveUserCreateNewCall(user);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("true", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals(user.toString(),mobileloginCall.getString("userId"), "验证uid");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(mobileloginCall.getString("userId")));
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), mobileloginCall.getString("accessToken"), "验证accessToken");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Mobilelogin_002() {
		Logger.start(true,"手机号区号不匹配失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode()+1);
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals("104", mobileloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Mobilelogin_003() {
		Logger.start(true,"不输手机号失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
//			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", mobileloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Mobilelogin_004() {
		Logger.start(true,"不输区号失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
			mobileloginBean.setMobile(u.getMobile());
//			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", mobileloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Mobilelogin_005() {
		Logger.start(true,"普通用户手机号登录失败");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			u.setMobile(TestDataManager.getPhoneNum());
			u.setMobileAreaCode("86");
			Integer user = DGUserCallService.UserCreateCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals("104", mobileloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Mobilelogin_006() {
		Logger.start(true,"验证码错误失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode()+1);
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals("106", mobileloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Mobilelogin_007() {
		Logger.start(true,"不输入验证码失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
//			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode()+1);
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", mobileloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Mobilelogin_008() {
		Logger.start(true,"不输入AppSource失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
//			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", mobileloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Mobilelogin_009() {
		Logger.start(true,"商家手机号登录成功-商家没有微信信息");
		try {
			 
//			confirmValidateCodeAndKeepNormalBean.setValidateCode(getValidateCodeCall.getValidateCode());
//			confirmValidateCodeAndKeepNormalBean.setValidateType(getvalidatecodeBean.getValidateType());
			
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setOpenId(null);
			Integer user = DGUserCallService.UserCreateCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("true", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals(user.toString(),mobileloginCall.getString("userId"), "验证uid");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(mobileloginCall.getString("userId")));
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), mobileloginCall.getString("accessToken"), "验证accessToken");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Mobilelogin_010() {
		Logger.start(true,"已删除商家手机号登录失败");
		try {
			
//			confirmValidateCodeAndKeepNormalBean.setValidateCode(getValidateCodeCall.getValidateCode());
//			confirmValidateCodeAndKeepNormalBean.setValidateType(getvalidatecodeBean.getValidateType());
			
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			DGUserCallService.DeleteUser(user);
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(u.getClientIp());
			mobileloginBean.setDeviceId(u.getPhysicsDeviceId());
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();			
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Mobilelogin_011() {
		Logger.start(true,"商家手机号登录自己的小程序，成功");
		try {
			CreateUserBackBean u = DGUserCallService.initUsercreateNewBean();
			u.setUserType(1);
			Integer user = DGUserCallService.UserCreateNewCall(u);
			DGUserCallService.ReceiveUserCreateNewCall(user);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID()+"");
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setWxAppName(user+"");
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();
			Logger.verifyEquals("true", mobileloginCall.getString("success"), "验证success");
			Logger.verifyEquals(user.toString(),mobileloginCall.getString("userId"), "验证uid");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(mobileloginCall.getString("userId")));
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), mobileloginCall.getString("accessToken"), "验证accessToken");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Mobilelogin_012() {
		Logger.start(true,"商家手机号登录别人的小程序，失败");
		try {
			CreateUserBackBean u = DGUserCallService.initUsercreateNewBean();
			u.setUserType(1);
			Integer user = DGUserCallService.UserCreateNewCall(u);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID()+"");
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setWxAppName("500326933");
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();	
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Mobilelogin_013() {
		Logger.start(true,"商家手机号登录公版小程序，失败");
		try {
			CreateUserBackBean u = DGUserCallService.initUsercreateNewBean();
			u.setUserType(1);
			Integer user = DGUserCallService.UserCreateNewCall(u);
			DGUserCallService.ReceiveUserCreateNewCall(user);
			//生成验证码 ActionUser：MobileAreaCode_Mobile validatetype: 17
			GetValidateCodeBean getvalidatecodeBean = new ValidateCodeServiceV2().setGetValidateCodeBean(1, 640, 4,u.getMobileAreaCode()+"_"+u.getMobile(), 17);
			GetValidateCodeCall getValidateCodeCall = new ValidateCodeServiceV2().GetValidateCode(getvalidatecodeBean);
			
			mobileloginBean.setAppSource(u.getAppSource());
			mobileloginBean.setClientIp(TestDataManager.getRandomIp());
			mobileloginBean.setDeviceId(TestDataManager.getRandomUUID()+"");
			mobileloginBean.setMobile(u.getMobile());
			mobileloginBean.setMobileAreaCode(u.getMobileAreaCode());
			mobileloginBean.setMobileVerifyCode(getValidateCodeCall.getValidateCode());
			mobileloginCall.setData(mobileloginBean);
			mobileloginCall.callService();
			Logger.verifyEquals("false", mobileloginCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}